package com.woniuxy.controller;

import com.woniuxy.pojo.Dept;
import com.woniuxy.pojo.Role;
import com.woniuxy.pojo.User;
import com.woniuxy.service.DeptService;
import com.woniuxy.service.RoleService;
import com.woniuxy.service.UserService;
import com.woniuxy.util.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController{
    private Logger logger= Logger.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;
    @GetMapping(value = "/selectAllUsers")
    public ModelAndView selectAllUsers(){
        ModelAndView mav=new ModelAndView();
        List<User> userList= userService.findAllUsers();
        mav.addObject("userList",userList);
        mav.setViewName("ulist");
        return mav;
    }
    /**
     * 进入增加用户的界面
     * 1）查询所有的角色信息，为了完成前台复选框的显示
     * 2）查询所有的部门信息，为了完成前台下拉菜单数据显示
     * @return
     */
    @GetMapping("/goAddUser")
    public String goAddUser(HttpServletRequest request){
        List<Dept> deptList=deptService.findAllDepts();
        List<Role> roleList=roleService.findAllRoles();
        request.setAttribute("deptList",deptList);
        request.setAttribute("roleList",roleList);
        return "uadd";
    }
    @PostMapping("/addUser")
    public String addUser(User user,
                          int[] roleIds,
                          @RequestParam("headImage") MultipartFile headImage,
                          HttpServletRequest request){
        //找到到服务器上的目录
        String uploadDir=request.getServletContext().getRealPath("/static/upload/");
        //获取文件的后缀
        String fileSuffix=headImage.getOriginalFilename().substring(headImage.getOriginalFilename().lastIndexOf(".")+1);
        logger.info("后缀名:"+fileSuffix);
        //构建时间戳
        String fileName= "img_"+DateUtil.timeStamp(new Date())+"."+fileSuffix;
        String imageFile=uploadDir+ File.separator+fileName;
        logger.info("文件的路径:"+imageFile);
        try {
            headImage.transferTo(new File(imageFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Role> roles=new ArrayList<>();
        if(roleIds!=null){
            for(Integer roleId:roleIds){
                Role role=new Role();
                role.setId(roleId);
                roles.add(role);
            }
        }
        user.setRoles(roles);
        user.setAvailable(1);
        //将图片的名称存放到数据库headImage字段中
        user.setHeadimg(fileName);
        //调用业务增加方法
        userService.addUser(user);
        return "redirect:selectAllUsers";
    }
}












